package com.smarthospital.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.common.Result.ResultDTO;
import com.smarthospital.contants.SHConst;
import com.smarthospital.entity.pojo.InquiryOrder;
import com.smarthospital.entity.pojo.User;
import com.smarthospital.service.OrderService;
import com.smarthospital.web.inteceptor.LoginInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.chrono.IsoChronology;
import java.util.concurrent.TimeUnit;

/**
 * create by sym  2020/8/16 16:00.
 */
@RestController
@Slf4j
@RequestMapping("/inqury")
@Api(description = "问诊单接口",value = "weChat")
public class InquryController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/list")
    @ApiOperation(value = "我的问诊单",
            notes =" 我的问诊单，根据医生等级分开，分页查询\n " +
                    "param: doctorLevel -> 医生级别 0_普通医生 1_专家" +
                    "       pageSize -> 页码大小\n" +
                    "       pageNum -> 当前页数" )
    public ResultDTO list(@RequestParam("doctorLevel")Integer doctorLevel,@RequestHeader("SH_TOKEN")String SH_TOKEN,
                           @RequestParam("pageSize")Integer pageSize, @RequestParam("pageNum")Integer pageNum){
        log.info("list doctorLevel {}  pageSize {} pageNum {}",doctorLevel,pageSize,pageNum );
        PageInfo<InquiryOrder> pageInfo = orderService.querMyInquiryOrder(doctorLevel,pageSize,pageNum);
        return ResultDTO.sucess(pageInfo);
    }

    @GetMapping("/getOrder")
    @ApiOperation(value = "根据id获取订单",
            notes =" 根据id获取订单\n " +
                    "param: id -> id\n")
    public ResultDTO getOrder(@RequestParam("id")Long id,@RequestHeader("SH_TOKEN")String SH_TOKEN){
        log.info("getOrder id {}  ",id);
        InquiryOrder order = orderService.getOrder(id);
        return ResultDTO.sucess(order);
    }

    @PostMapping("/addOrder")
    @ApiOperation(value = "提交订单 正常提交订单不用这个",
            notes =" 提交订单,未付款,30分钟内付款有效\n" )
    public ResultDTO addOrder(@RequestBody InquiryOrder order,@RequestHeader("SH_TOKEN")String SH_TOKEN){
        User user = LoginInterceptor.getUser();
        String key = String.format(SHConst.redis_key.addOrder,user.getId()) ;
        if (redisTemplate.opsForValue().setIfAbsent(key,"1",1,TimeUnit.SECONDS)) {
            throw new CommonException(CodeMsg.ERROR_OP_FAST);
        }
        log.info("addOrder order {}  ", JSON.toJSONString(order));
        orderService.addOrder(order);
        return ResultDTO.sucess();
    }

    @PostMapping("/updateOrder")
    @ApiOperation(value = "修改订单信息",
            notes =" 支持修改部分订单信息\n" )
    public ResultDTO updateOrder(@RequestBody InquiryOrder order,@RequestHeader("SH_TOKEN")String SH_TOKEN){
        User user = LoginInterceptor.getUser();
        String key = String.format(SHConst.redis_key.updateOrder,user.getId()) ;
        if (redisTemplate.opsForValue().setIfAbsent(key,"1",1,TimeUnit.SECONDS)) {
            throw new CommonException(CodeMsg.ERROR_OP_FAST);
        }
        log.info("updateOrder order {}  ", JSON.toJSONString(order));
        orderService.updateOrder(order);
        return ResultDTO.sucess();
    }

    @GetMapping("/complateOrder")
    @ApiOperation(value = "关闭订单",
            notes ="param: id -> id\n")
    public ResultDTO complateOrder(@RequestParam("id")Long id,@RequestHeader("SH_TOKEN")String SH_TOKEN){
        log.info("complateOrder id {}  ",id);
        User user = LoginInterceptor.getUser();
        String key = String.format(SHConst.redis_key.updateOrder,user.getId()) ;
        if (redisTemplate.opsForValue().setIfAbsent(key,"1",1L,TimeUnit.SECONDS)) {
            throw new CommonException(CodeMsg.ERROR_OP_FAST);
        }
        orderService.complateOrder(id);
        return ResultDTO.sucess();
    }


}
